<?
    /*
        SettingManager is a peculiar beast.  It takes advantage of some of PHPs unique abilities, for example,
        SettingManager can be called from either a static or object context, depending on the functionality 
        needed.  Take the following examples, all of which do the same thing:
        
        ** The most basic call - this is what every call gets reduced to eventually
            SettingManager::GetSetting("guest_accounts_enabled");
        
        ** The next most basic call - the string is encoded into guest_accounts_enabled
            SettingManager::GetSetting("Guest Accounts Enabled");
            
        ** This is how you can use the setting manager in an object context to do less
        ** overall typing.  This uses decreased verbosity per call but not overall, therefore
        ** this method is recommended if there are several settings in a group that you want to 
        ** reference
            $guestSettings = new SettingManager();
            $guestSettings->AccountsEnabled();
        ** If you wanted to use a lot of settings in a group, take the following example
            $mailsettings = new SettingManager("mail");
            $mailsettings->Enabled();               // "mail_enabled"
            $mailsettings->ServerName;              // "mail_server_name"
            $mailsettings->Port;
        ** When initiated in a static context, the object will not try to load any variables 
        ** unless the subsetting xxx_enabled is set in the database.
        
        ** Using it in object context
            $gsettings = new SettingManager("guest");
            $gsettings->AccountsEnabled();
            $gsettings->AccountsEnabled;
            
        Since the setting in question runs through a parser first, the proper setting name can be 
        built using different aspects of the object.
        
        
        
        NOTE: This version of SettingManager makes use of the DatabaseManager class to load and store
              data -> you may need to change this to match your particular implementation.
    */
    class SettingManager {
        private static $_settings = array();
        private static $_custom_settings_panels = array();
        private $_prefix;
        
        // Object Context
        function __construct($subsetting) {
            $this->_prefix = $subsetting . "_";
        }
        function GetSubSetting($setting) {
            return self::GetSetting($this->_prefix . $setting);
        }
        
        // magic functionality
        function __get($var) {
            return $this->GetSubSetting($var);
        }
        function __call($func,$args) {
            $output = '';
            // convert the function name into a setting name
            // ThisIsASetting() becomes this_is_a_setting
            // basically - any way that you can legally call a function will return the right variable
            for ($x = 0; $x < strlen($func); $x++) {
                if (ord($func[$x]) == ord(strtoupper($func[$x]))) $output .= '_';
                $output .= $func[$x];
            }
            return $this->GetSubSetting($output);
        }
        // static magic
        // only implemented in php 5.3+ so it's commented out by default
        /*
        function __callStatic($func,$args) {
            $output = '';
            for ($x = 0; $x < strlen($func); $x++) {
                if (ord($func[$x]) == ord(strtoupper($func[$x]))) $output .= '_';
                $output .= $func[$x];
            }
            return self::GetSetting($output);
        }
        */
        
        // Static Context
        static function LoadSettings() {
            $settings = DatabaseManager::FetchAllRows("SELECT `setting_name`,`setting_value` FROM `settings` ORDER BY `setting_name` ASC;");
            
            foreach ($settings as $setting) {
                self::$_settings[$setting['setting_name']] = $setting['setting_value'];
            }
        } 
        static function IsEnabled($var) {
            return self::GetSetting($var . "_enabled");
        }
        static function EncodeSettingName($str) {
            $str = strtolower($str);
            $replaced = "";
            $validchars = "abcdefghijlkmnopqrstuvwxyz0123456789_";
            for ($x = 0; $x < strlen($str); $x++) {
                if (strpos($validchars,$str[$x]) !== false) $replaced .= $str[$x];
                else if ($str[$x] == " ") $replaced .= "_";
            }
            return $replaced;
        }
        static function GetRawSetting($setting) {
            return self::$_settings[$setting];
        }
        static function GetSetting($setting) {
            if (!isset(self::$_settings[$setting])) $setting = self::EncodeSettingName($setting);            
            return DynaText::BindSettings(self::$_settings[$setting]);
        }
        static function GetSettings() { return self::$_settings; }
        static function AddCustomPanel(OptionsPanel $panel) {
            self::$_custom_settings_panels[] = $panel;
        }
        static function GetCustomPanels() { return self::$_custom_settings_panels; }
    }
?>